<template>
  <div>
    <new-search-bar :searchFixed="true" bgSearch="c-bg-white c-pv24-i" @search="startSearch" @cancel="startSearch"></new-search-bar>
    <div class="c-pt24" v-for="(item,index) in examinationList" :key="index" @click="clickExaminationItem(item.topicId)">
      <examination-item :item="item"></examination-item>
      <div class="c-bd-b1 c-pt24 c-mh24"></div>
    </div>
    <loading-status-tem :showLoading="isLoading" :noMoreData="is_page == false && (examinationList && examinationList.length > 0)" :noData="!examinationList || examinationList.length == 0" :noDataText="'暂无结果'"></loading-status-tem>
    <div v-if="companyAuth != null && companyAuth.companyNav != null">
      <nav-bottom-bar :itemIndex="8" v-for="(item,index) in companyAuth.companyNav" :key="index" v-if="item.navType == 8"></nav-bottom-bar>
    </div>
  </div>
</template>

<script>
import loadingStatusTem from "@/components/templates/common/loadingStatusTem.vue";
import newSearchBar from "@/components/templates/common/newSearchBar.vue"
import ExaminationItem from "@/components/templates/examinationItem.vue";
import NavBottomBar from "@/components/templates/common/navBottomBar.vue";
import { utilJs } from "@/utils/common.js"

export default {
  name: "ExaminationList",
  components: {
    loadingStatusTem,
    ExaminationItem,
    NavBottomBar,
    newSearchBar,
  },
  props: [],
  data() {
    return {
      examinationList: [],
      is_page: false,
      page: 1,
      limit: 10,
      searchText: '',
      isLoading: false,
      companyAuth: localStorage.getItem("companyAuth") ? JSON.parse(localStorage.getItem("companyAuth")) : null,
    };
  },
  created() {
    utilJs.appShare(this);
  },
  activated() {
    setDocumentTitle("测评列表");// eslint-disable-line
    this.isLoading = false;
    // this.page = 1;
    // this.searchText = '';
    // this.examinationList = [];
    if (utilJs.checkNeedInitData(this.examinationList)) {
      this.initData()
    }
    // this.getMore();
    this.wechatShare();
    window.addEventListener('scroll', this.windowScrollFunction);
  },
  deactivated() {
    // this.is_page = false;
    window.removeEventListener('scroll', this.windowScrollFunction);
  },
  mounted() {

  },
  methods: {
    initData() {
      this.page = 1;
      this.searchText = "";
      this.is_page = false;
      this.examinationList = [];
      this.getMore();
    },
    startSearch(e) {
      this.page = 1;
      this.examinationList = [];
      this.searchText = e.searchValue;
      this.getMore();
    },
    clickExaminationItem(examinationNo) {
      this.$routerGo(this, "push", {
        path: "/homePage/examination/examinationDetail",
        query: { ExaminationNo: examinationNo }
      });
    },
    getMore() {
      if (!this.isLoading) {
        this.isLoading = true;
        let $this = this;
        utilJs.getMethod(global.apiurl + 'exam?searchName=' + this.searchText + '&page=' + this.page + '&limit=' + this.limit, function (res) {
          $this.companyAuth = JSON.parse(localStorage.getItem("companyAuth"));
          $this.isLoading = false;
          $this.examinationList = $this.examinationList.concat(res.data);
          $this.page = res.data && (res.data.length >= $this.limit) ? ($this.page + 1) : $this.page;//res.current_page < res.last_page ? ($this.page + 1) : $this.page;
          $this.is_page = res.data && (res.data.length >= $this.limit) ? true : false;//res.current_page < res.last_page ? true : false;
        },
          failRes => {
            $this.companyAuth = JSON.parse(localStorage.getItem("companyAuth"));
            $this.isLoading = false;
          }
        );
      }
    },
    //分享
    wechatShare: function () {
      let title = "测评列表";//window.localStorage.getItem("shareTitle");
      let shareUrl = `${window.location.href.split("#")[0]}#${this.$route.path}?refereeId=${localStorage.getItem("userId")}`;
      utilJs.wechatConfig(shareUrl, title, "", "", function () { });
    },
    //手机端分享
    appShare: function () {
      let title = "测评列表";//window.localStorage.getItem("shareTitle");
      let shareUrl = `${window.location.href.split("#")[0]}#${this.$route.path}?refereeId=${localStorage.getItem("userId")}`;
      utilJs.appShareTrue(title, "", shareUrl, "");
    },
    windowScrollFunction: function () {
      let that = this;
      utilJs.handleLoading(function () {
        if (that.is_page == true) {
          that.getMore();
        }
      });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->

<style scoped>
</style>